<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class ApiLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        $log = "\r\n".'[URL]' . $request->url();
        $log .= "\r\n".'[TIME]' . now();
        $log .= "\r\n".'[IP]' . $request->ip();
        $log .= "\r\n".'[USER]' . json_encode($request->user(), JSON_UNESCAPED_UNICODE);
        $log .= "\r\n".'[REQ]' . json_encode($request->all(), JSON_UNESCAPED_UNICODE);
        $log .= "\r\n".'[RES]' . $response->getContent();

        Log::channel('api')->debug($log);
        return $response;
    }
}
